#include "CWave.h"
#include "CGameApp.h"
#include "CPlayer.h"
#include <stdlib.h>
#include <time.h>
using namespace std;

CWave::CWave()
:Enemy(NULL),
NumEnemies(20),
EnemiesRemaining(NumEnemies),
pParent(NULL)
{
	Enemy = new CEnemy*[NumEnemies];
	for( int i = 0; i < NumEnemies; i++ )
	{
		Enemy[i] = new CEnemy();
	}
}

CWave::CWave( int enemycount )
:Enemy(NULL),
NumEnemies(enemycount),
EnemiesRemaining(NumEnemies),
pParent(NULL)
{
	Enemy = new CEnemy*[NumEnemies];
	for( int i = 0; i < NumEnemies; i++ )
	{
		Enemy[i] = new CEnemy();
	}
}

CWave::~CWave() 
{
	if(Enemy)
	{
		for( int i = 0; i < NumEnemies; i++ )
		{
			delete Enemy[i];
		}
		delete[] Enemy;
	}
}

void CWave::Render()
{
	for( int i = 0; i < NumEnemies; i++ )
	{
		Enemy[i]->Render();
	}
}

bool CWave::Update()
{

//	if( pParent->KeyState['z'] == true )
//	{
//		CWeapon* wpn = new CWeapon( 10, 5 );
//		wpn->Attach( Enemy[0] );
//		//load won't work unless it has a ship attached to it
//		wpn->Load();
//		wpn->SpreadAngle = 9.0f;
//		wpn->RateOfFire = 15.0f;
//		Enemy[0]->EquipPrimary( wpn );
//		//CWeapon* wpn = new CWeapon( 10, 5 );
//		//wpn->Attach( Player );
//		////load won't work unless it has a ship attached to it
//		//wpn->Load();
//		//wpn->SpreadAngle = 9.0f;
//		//wpn->RateOfFire = 15.0f;
//		//Player->EquipPrimary( wpn );
//	}
////
//	if( KeyState['x'] == true )
//	{
//		CWeapon* wpn = new CHomingWeapon( 10, 3 );
//		wpn->Attach( Enemy[1] );
//		//load won't work unless it has a ship attached to it
//		wpn->Load();
//		wpn->SpreadAngle = 30.0f;
//		//wpn->RateOfFire = 15.0f;
//		Enemy[1]->EquipPrimary( wpn );
//	}
//
	if( pParent->KeyState[32] == true ) //spacebar
	{
		for( int i = 0; i < NumEnemies; i++ )
		{
			//Enemy[i]->FireBullet();
		}
	}

	//enemy movement
	int count = 0;

	for( int i=0; i<NumEnemies; i++ )
	{
		//Enemy[i]->FireBullet();
		
		if( Enemy[i]->GetAlive() == true )
		{
			//Enemy[i]->TimeAlive += pParent->DeltaTime;
			//Enemy[i]->SetOldPos( Enemy[i]->GetPosX(), Enemy[i]->GetPosY() );

			//Enemy[i]->UpdateAction();
			//if(	Enemy[i]->UpdateAction() == false ) { Enemy[i]->PerformAction( Enemy[i]->m_fFaceAngle , 2112.0f , 2.0f ); }
			//Enemy[i]->MovePosX( pParent->DeltaTime*Enemy[i]->GetSpeed()*Enemy[i]->GetVelX() );
			//Enemy[i]->MovePosY( pParent->DeltaTime*Enemy[i]->GetSpeed()*Enemy[i]->GetVelY() );
			//if( Enemy[i]->GetPosY() < -9 ) { Enemy[i]->SetVel( Enemy[i]->GetVelX(), 1  ); }//FaceAngle( 90 ); }
			//if( Enemy[i]->GetPosY() >  9 ) { Enemy[i]->SetVel( Enemy[i]->GetVelX(), -1 ); }//SetFaceAngle( 270 ); }
			//if( Enemy[i]->TimeAlive >= 1.0f )// && Enemy[i]->TimeAlive < 20.1f )
			//{
			//	//Enemy[i]->SetFaceAngle( 270 );
			//	Enemy[i]->SetFaceAngle( pParent->Player->GetPosX(), pParent->Player->GetPosY() );
			//	//RotateFaceAngleBy( pParent->DeltaTime*GetRotateSpeed() * 360 );
			//}
			//else//if( Enemy[i]->TimeAlive < 1.0f )
			//{
			//	Enemy[i]->RotateFaceAngleBy( -pParent->DeltaTime*Enemy[i]->GetRotateSpeed() * 360 );
			//	//SetFaceAngle( pParent->Player->GetPosX(), pParent->Player->GetPosY() );
			//	Enemy[i]->SetOldPos( Enemy[i]->GetPosX(), Enemy[i]->GetPosY() );
			//	//cout << GetVelX() << " " << GetVelY() << endl;
			//	Enemy[i]->MovePosY( pParent->DeltaTime*Enemy[i]->GetSpeed()*Enemy[i]->GetVelY() );
			//	Enemy[i]->MovePosX( pParent->DeltaTime*Enemy[i]->GetSpeed()*Enemy[i]->GetVelX() );
			//	Enemy[i]->FireBullet();
			//}
		}
		Enemy[i]->Update();
		if( Enemy[i]->GetAlive() == false && Enemy[i]->GetEAlive() == false && Enemy[i]->Waiting == false )
		{
			//EnemiesRemaining -= 1;
			count += 1;
			//Enemy[i]->SetPos( rand()%5*2.5f-5 , 11 );//-9.0f + (i%10)*2, 7.0f - (i/10)*2 );//rand() % 19 - 9 , 9 );//-9.0f + i*2, 7.0f );
			//Enemy[i]->SetOldPos( Enemy[i]->GetPosX(), Enemy[i]->GetPosY() ); 
			//Enemy[i]->SetAlive( true );
			//Enemy[i]->SetHealth( 1 );
			//Enemy[i]->SetDAlive( false );
			//EnemiesRemaining += 1;
		}
	}//next enemy
	if( count < NumEnemies )
	{ return true; }
	else
	{ return false; }

	//collision checks on everything
//	CheckCollision();
}

void CWave::Initialize()
{
	if( pParent == NULL ) { cout << "CWave::Initialize failed. GameApp was null " << endl; exit(0);}
	
	NumEnemies = 2;
	pParent->NumEnemies = NumEnemies;
	Enemy[0]->SetPos(8, 94);
	Enemy[1]->SetPos(18, 94);
	for( int i = 0; i < NumEnemies; i++ )
	{
		Enemy[i]->SetGame( GetGame() );
		Enemy[i]->Load();
	}
	Enemy[0]->SetPos(8, 94);
	Enemy[1]->SetPos(18, 94);
	for( int i = 0; i < NumEnemies; i++ )
	{
		//Enemy[i]->SetPos( 8 + i*10 , 94 );//-9.0f + (i%10)*2, 7.0f - (i/10)*2 );//rand() % 19 - 9 , 9 );//-9.0f + i*2, 7.0f );
		Enemy[i]->SetOldPos( Enemy[i]->GetPosX(), Enemy[i]->GetPosY() );
		//Enemy[i]->SetSpeed(10);
		Enemy[i]->SetAlive( false );
		Enemy[i]->Waiting = true;
		Enemy[i]->TimeUntilAlive = 1;//float(rand()%20)/3.0f + 2.0f;//i*0.5f+1;//1.0f * (i+1);
		Enemy[i]->SetParentWave( this );
		//cout << "enemy damage " << Enemy[i]->GetDamage() << "enemyhealth" << Enemy[i]->GetHealth() << endl;
		//Enemy[i]->PerformAction( Enemy[i]->Pos.y , -5.0f, 8.0f );
		//Enemy[i]->PerformAction( Enemy[i]->m_fFaceAngle , 720.0f, 3.0f );//4.0f );
	}//next enemy
}

void CWave::Collision( CPlayer* player )
{

	for( int i = 0; i < NumEnemies; i++ )
	{
		if( COLLISION( player , Enemy[i] ) )
		{ 
			float nx = player->GetPosX() - Enemy[i]->GetPosX();
			float ny = player->GetPosY() - Enemy[i]->GetPosY();
			if( nx > 0 )
			{
				if( ny/nx > 1 ) //player wins
				{
					Enemy[i]->Kill();
				}
				else //enemy won
				{
					player->Kill();
				}
			}
			if( nx < 0 )
			{
				if( ny/nx < -1 ) //player wins
				{
					Enemy[i]->Kill();
				}
				else //enemy won
				{
					player->Kill();
				}
			}

			//player->TakeDamage(Enemy[i]->GetDamage()); 
			//Enemy[i]->TakeDamage(player->GetDamage()); 
		}
		
		//for each player bullet
		for( int x = 0; x < player->PrimaryWeapon->NumBullets; x++ )
		{
			//if the enemy died with a previous bullet, skip to next enemy
			if( Enemy[i]->GetAlive() == false ) { break; }
			//for each bullet in the player spread
			for( int y = 0; y < player->PrimaryWeapon->Spread; y++ )
			{
				if( Enemy[i]->GetAlive() == false ) { break; }
				//if( Player->PrimaryWeapon->Bullets[x][y]->GetAlive() == false ) { continue; }
				//mine = Player->PrimaryWeapon->Bullets[x][y]->GetShape();
				//yours = Enemy[i]->GetShape();
				//memptr = &Player->PrimaryWeapon->Bullets[x][y]->Collision[mine][yours];
				//if( COLLISION( player->PrimaryWeapon->Bullets[x][y] , Enemy[i] ) )
				//{
				//	CEntity* pb = player->PrimaryWeapon->Bullets[x][y]; //pb means player bullet
				//	cout << pb->GetType() << endl;
				//	if( pb->GetType() == "FriendWave" )
				//	{
				//		player->PrimaryWeapon->Bullets[x][y]->TakeDamage(Enemy[i]->GetDamage());
				//		Enemy[i]->TakeDamage(player->PrimaryWeapon->Bullets[x][y]->GetDamage());
				//	}
				//}//end if there's a collision
				if( COLLISION( player->SecondaryWeapon->Bullets[x][y] , Enemy[i] ) )
				{
					CEntity* pb = player->SecondaryWeapon->Bullets[x][y]; //pb means player bullet
					//cout << pb->GetType() << endl;
					//if( pb->GetType() == "FriendWave" )
					{
						player->SecondaryWeapon->Bullets[x][y]->TakeDamage(Enemy[i]->GetDamage());
						Enemy[i]->TakeDamage(player->PrimaryWeapon->Bullets[x][y]->GetDamage());
					}
				}//end if there's a collision
			}//next spread bullet
		}//next row of bullets

		//check collision for the enemies bullets and the player/player bullets
		//if the enemy is dead, it and its bullets can't collide with anything
		//if( Enemy[i]->GetAlive() == false ) { continue; }
		for( int x = 0; x < Enemy[i]->PrimaryWeapon->NumBullets; x++ )
		{
			for( int y = 0; y < Enemy[i]->PrimaryWeapon->Spread; y++ )
			{
				//if( Enemy[i]->PrimaryWeapon->Bullets[x][y]->GetAlive() == false ) { continue; }
				//if( Player->GetAlive() == false ) { continue; }
				//mine = Enemy[i]->PrimaryWeapon->Bullets[x][y]->GetShape();
				//yours = Player->GetShape();
				//CEntity* EnemyBullet = Enemy[i]->PrimaryWeapon->Bullets[x][y];
				if( COLLISION( Enemy[i]->PrimaryWeapon->Bullets[x][y] , player ) )
				//if( CALL_FNP( EnemyBullet, EnemyBullet->Collision[mine][yours] )(Player) )
				//if( Enemy[i]->PrimaryWeapon->Bullets[x][y]->CircleCircleCollision(Player) )
				//if( Enemy[i]->PrimaryWeapon->Bullets[x][y]->Collision[(Enemy[i]->PrimaryWeapon->Bullets[x][y]->GetShape())][(Player->GetShape())](Player) )
				{
					Enemy[i]->PrimaryWeapon->Bullets[x][y]->Kill();
					player->TakeDamage(Enemy[i]->PrimaryWeapon->Bullets[x][y]->GetDamage());
				}
				//check for collision with the players bullets and the enemies bullets
#if 0			
				for( int xp = 0; xp < player->PrimaryWeapon->NumBullets; xp++ )
				{
					for( int yp = 0; yp < player->PrimaryWeapon->Spread; yp++ )
					{
						//if( Player->PrimaryWeapon->Bullets[xp][yp]->GetAlive() == false ) { continue; }
						//if( Enemy[i]->PrimaryWeapon->Bullets[x][y]->GetAlive() == false ) { continue; }
						//mine = Enemy[i]->PrimaryWeapon->Bullets[x][y]->GetShape();
						//yours = Player->PrimaryWeapon->Bullets[xp][yp]->GetShape();
						//CEntity* EnemyBullet = Enemy[i]->PrimaryWeapon->Bullets[x][y];
						if( COLLISION( Enemy[i]->PrimaryWeapon->Bullets[x][y] , player->PrimaryWeapon->Bullets[xp][yp] )  )
						//if( CALL_FNP( EnemyBullet , EnemyBullet->Collision[mine][yours] )(Player->PrimaryWeapon->Bullets[xp][yp]) )
						//if( Enemy[i]->PrimaryWeapon->Bullets[x][y]->CircleCircleCollision(Player->PrimaryWeapon->Bullets[xp][yp]) )
						{
							Enemy[i]->PrimaryWeapon->Bullets[x][y]->TakeDamage(player->PrimaryWeapon->Bullets[xp][yp]->GetDamage());
							player->PrimaryWeapon->Bullets[xp][yp]->TakeDamage(Enemy[i]->PrimaryWeapon->Bullets[x][y]->GetDamage());
						}
					}//next player bullet spread
				}//next player bullet row
#endif

			}//next bullet in the enemy's weapon spread
		}//next bullet in the enemy's weapon array


	}//next enemy


	//player collision with all the enemies

	//player weapon bullet with each enemy

	//enemy bullet and player
	//enemy bullet and player bullet

}

CEnemy* CWave::FindTarget()
{
	for( int i = 0; i < NumEnemies; i++ )
	{
		if( Enemy[i]->GetAlive() )
		{
			return Enemy[i];
		}
	}
	return NULL;
}










